/**
   DanmuTestContoller
 * Time:2018年6月20日
 */
package com.ssm.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder.In;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ssm.entity.Page;
import com.ssm.entity.PageCount;
import com.ssm.entity.Testdanmu;
import com.ssm.entity.vo.PageNumVo;
import com.ssm.service.DanmuService;
import com.ssm.service.DanmuTestService;

/**
 * @author 15199
 *
 */
@Controller
@RequestMapping("/DanmuTest")
public class DanmuTestContoller {

	@Resource	
	private DanmuTestService danmuTestService;


    /*保存弹幕*/
	@RequestMapping("/save.do")	
	@ResponseBody
	public String save(@RequestBody Testdanmu danmu){


		int n=danmuTestService.saveDanmu(danmu);

		if(n>0){
			return "true";
		}else{
			return "false";
		}
	}
	/*查询出所有的弹幕转发给前端*/
	@RequestMapping("/show.do")
	@ResponseBody
	public List<Testdanmu> show(){
		List<Testdanmu> list=new ArrayList<Testdanmu>();

		list=danmuTestService.showDanmu();


		return list;


	}
	/*弹幕列表*/
	@RequestMapping("/list.do")
	@ResponseBody
	public List<Testdanmu> list(@RequestBody PageNumVo pageNumvo){

		Page page=parsePageNo(pageNumvo.getPageNum());


		List<Testdanmu> list=new ArrayList<Testdanmu>();

		list=danmuTestService.list(page,new Testdanmu());	

		return list;
	}
	/*获取最大页数*/
	@RequestMapping("/getPageNum.do")
	@ResponseBody
	public int getPageNum(){
		
    int n=danmuTestService.getPageNum();	
		
	if(n>0){
	
	PageCount pageCount=new PageCount(n,5);
	
		
	return pageCount.getPageMax(); 	
	}
	
	return 0;
	
	}
	
	
	
	
	


	protected Page parsePageNo(String parmes) {


		Page page = new Page();
		page.setLength(5);
		if (StringUtils.isEmpty(parmes)){
			page.setPageNo(1);
		} else {
			page.setPageNo(Integer.parseInt(parmes));
		}

		return page;
	}
	/*根据前台传过来的参数删除弹幕*/
	@RequestMapping("/delete.do")
	@ResponseBody
	public String delete(@RequestBody String id){


		int n=danmuTestService.deleteDanmu(parseInt(id));
		if(n>0){
			return "true";
		}else{
			return "false"; 
		}


	}
	/*根据传过来的json字符串来批量删除数据*/
	@RequestMapping("/batchdelete.do")
	@ResponseBody
	public String batchdelete(@RequestBody List<String> list){
		int n=0;
		for (String str : list) {
		n+=danmuTestService.deleteDanmu(parseInt(str));
		}
		if(n==5){
			return "true";
		}else{
			return "false"; 	
		}		
	}
	
	protected int parseInt(String id){
		int i;
		if(!StringUtils.isEmpty(id)){
			i=Integer.parseInt(id);
			return i;	
		}else{
			return 0;
		}
	}
	
	
	
	
	
    

	




}
